<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%@ taglib prefix="fns" uri="/WEB-INF/tlds/fns.tld"%>
<!DOCTYPE html>
<html>
	<head>
		<title>角色添加</title>
		<!-- 引入公用Js与Css -->
  		<%@include file="/view/common/resource.jsp" %>
	</head>
	<body>
		<div id="wrap">
			
			<div class="clearfix mt10 mr10 ml10">
				<form id="roleForm" class="form-horizontal" action="<%=basePath%>/sys/role/addSave" method="post">
					<input type="hidden" name="id" value="${role.id }">
					<dl class="ml10">
						<dd class="clearfix pt5">
							<label class="w100 pull-left text-right line-height30">角色名称:</label>
							<div class="w300 input-group input-group-sm">
								<input type="text" id="roleName" name="roleName" value="${role.roleName }" class="form-control"  title="请填写角色的名称">
							</div>
						</dd>
						<dd class="clearfix pt5">
							<label class="w100 pull-left text-right line-height30">角色编码:</label>
							<div class="w300 input-group input-group-sm">
								<input type="text" id="roleCode" name="roleCode" value="${role.roleCode }" class="form-control"  title="请填写角色的编码">
							</div>
						</dd>
					
						<dd class="clearfix pt30 ml110">
							<p class="radius3 bg_blue text-center w200 line-height30 mt10">
								<a href="javascript:" onclick="updateSave();" class="color_white show">确认更新</a>
							</p>
						</dd>
					</dl>
				</form>
			</div>
		</div>
		<script type="text/javascript">
	    var validate = null;
	    $(document).ready(function(){
	    	validate = $('#roleForm').validate({
	    		rules:{
					roleName:{
						required:true,
						remote:{
							url:'checkRoleExist',
							type:'post',
							data:{
								roleName:function(){return encodeURI(encodeURI($("#roleName").val()));},
								id:'${role.id }'
	    					}
						}
					},
					roleCode:{
						required:true,
						remote:{
							url:'checkRoleExist',
							type:'post',
							data:{
								roleCode:function(){return encodeURI(encodeURI($("#roleCode").val()));},
								id:'${role.id }'
	    					}
						}
					}
				},
				messages:{
					roleName:{
						required:"角色名称不能为空",
						remote:"角色名称不能重复"
					},
					roleCode:{
						required:"角色编码不能为空",
						remote:"角色编码不能重复"
					}
				},
				errorPlacement: function(error, element) {
					layer.tips(error.text(), '#'+element.attr("name"), {
					  tips: [4, '#78BA32']
					});
				}
			});
	    });
	    
    	/* 注册数据保存 */
	    function updateSave() {
    		
    		// 验证数据
	    	if (!validate.form()) {
	    		return false;
	    	}
	    	
    	    var actionurl=$('#roleForm').attr('action');//提交路径
    	    
    	    $("#roleForm").ajaxSubmit({
    	    	type:"post",
    	    	url:actionurl,
    	    	dataType: "json",
    	    	success:function(data){
        	    	if (data.success) {
    	   	        	// 成功后跳转首页
    	   	            layer.msg("角色更新成功", {icon: 6}, function() {
    	   	            	var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
    	   	            	parent.layer.close(index);
    	   	            });
    	   	        } else {
    	   	      		// 失败提醒
    	   	        	layer.msg(data.msg, {icon: 5});
    	   	        }
       	    	}
    	    }); 
	    }
    </script>
	</body>
</html>
